<?php

namespace common\models\app;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%app_boot_splash}}".
 *
 * @property int $id
 * @property string $name 名称
 * @property string $format 格式
 * @property string $content 内容
 * @property int $begin_time 开始时间
 * @property int $end_time 展示截止时间
 * @property int $listorder 展示排序
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class AppBootSplash extends \common\base\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\TimestampBehavior;

    /**
     * status attributes
     * 状态
     * -1已下架，0未启用，1已启用
     */
    CONST STATUS_CLOSED = -1;
    CONST STATUS_DEFAULT = 0;
    CONST STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%app_boot_splash}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name', 'content'], 'required'],
            [['begin_time', 'end_time', 'listorder', 'status', 'created_at', 'updated_at'], 'integer'],
            [['name'], 'string', 'max' => 100],
            [['format'], 'string', 'max' => 20],
            [['content'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '名称',
            'format' => '格式',
            'content' => '内容',
            'begin_time' => '开始时间',
            'end_time' => '展示截止时间',
            'listorder' => '展示排序',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public static function getLatest() {
        $splash = static::find()
        ->where([
            'format' => 'image',
            'status' => static::STATUS_ACTIVE
        ])
        ->orderBy([
            'listorder' => SORT_ASC,
            'created_at' => SORT_DESC,
        ])
        ->one();
        return $splash ? Url::toRemote($splash->content) : '';
    }

}
